<template>
  <div class="admin-settings">
    <div class="page-header">
      <h2>系统设置</h2>
      <div class="header-actions">
        <button class="admin-btn admin-btn-primary" @click="saveSettings">保存设置</button>
      </div>
    </div>
    
    <div class="loading-container" v-if="loading">
      <div class="loading-spinner"></div>
      <p>加载系统设置中...</p>
    </div>
    
    <div class="settings-container" v-else>
      <!-- 基本设置 -->
      <div class="settings-section">
        <h3 class="section-title">基本设置</h3>
        
        <div class="setting-item">
          <label class="setting-label">系统名称</label>
          <input 
            type="text" 
            v-model="settings.systemName" 
            class="admin-form-input"
            placeholder="系统名称"
          >
          <div class="setting-desc">显示在页面标题和登录页</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">Logo URL</label>
          <div class="logo-input">
            <input 
              type="text" 
              v-model="settings.logoUrl" 
              class="admin-form-input"
              placeholder="Logo图片URL"
            >
            <button class="admin-btn">上传</button>
          </div>
          <div class="logo-preview" v-if="settings.logoUrl">
            <img :src="settings.logoUrl" alt="Logo预览">
          </div>
          <div class="setting-desc">系统Logo，建议尺寸200x50像素</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">维护模式</label>
          <div class="switch-container">
            <label class="switch">
              <input type="checkbox" v-model="settings.maintenanceMode">
              <span class="slider round"></span>
            </label>
            <span class="switch-status">{{ settings.maintenanceMode ? '开启' : '关闭' }}</span>
          </div>
          <div class="setting-desc">开启后，普通用户将无法访问系统</div>
        </div>
        
        <div class="setting-item" v-if="settings.maintenanceMode">
          <label class="setting-label">维护信息</label>
          <textarea 
            v-model="settings.maintenanceMessage" 
            class="admin-form-input"
            placeholder="系统维护中，请稍后再试..."
            rows="3"
          ></textarea>
          <div class="setting-desc">维护期间向用户显示的信息</div>
        </div>
      </div>
      
      <!-- 用户设置 -->
      <div class="settings-section">
        <h3 class="section-title">用户设置</h3>
        
        <div class="setting-item">
          <label class="setting-label">允许注册</label>
          <div class="switch-container">
            <label class="switch">
              <input type="checkbox" v-model="settings.allowRegistration">
              <span class="slider round"></span>
            </label>
            <span class="switch-status">{{ settings.allowRegistration ? '开启' : '关闭' }}</span>
          </div>
          <div class="setting-desc">是否允许新用户注册</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">注册需审核</label>
          <div class="switch-container">
            <label class="switch">
              <input type="checkbox" v-model="settings.requireApproval">
              <span class="slider round"></span>
            </label>
            <span class="switch-status">{{ settings.requireApproval ? '开启' : '关闭' }}</span>
          </div>
          <div class="setting-desc">新用户注册是否需要管理员审核</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">默认用户积分</label>
          <input 
            type="number" 
            v-model.number="settings.defaultPoints" 
            class="admin-form-input"
            min="0"
          >
          <div class="setting-desc">新用户注册时获得的初始积分</div>
        </div>
      </div>
      
      <!-- 挑战设置 -->
      <div class="settings-section">
        <h3 class="section-title">挑战与任务设置</h3>
        
        <div class="setting-item">
          <label class="setting-label">挑战审核</label>
          <div class="switch-container">
            <label class="switch">
              <input type="checkbox" v-model="settings.challengeRequireReview">
              <span class="slider round"></span>
            </label>
            <span class="switch-status">{{ settings.challengeRequireReview ? '开启' : '关闭' }}</span>
          </div>
          <div class="setting-desc">用户创建的挑战是否需要审核</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">任务自动审核</label>
          <div class="switch-container">
            <label class="switch">
              <input type="checkbox" v-model="settings.autoApproveSubmissions">
              <span class="slider round"></span>
            </label>
            <span class="switch-status">{{ settings.autoApproveSubmissions ? '开启' : '关闭' }}</span>
          </div>
          <div class="setting-desc">是否自动审核用户提交的任务</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">任务图片上传数量限制</label>
          <input 
            type="number" 
            v-model.number="settings.maxTaskImages" 
            class="admin-form-input"
            min="1"
          >
          <div class="setting-desc">用户提交任务时最多可上传的图片数量</div>
        </div>
      </div>
      
      <!-- 系统配置 -->
      <div class="settings-section">
        <h3 class="section-title">系统配置</h3>
        
        <div class="setting-item">
          <label class="setting-label">数据缓存时间(分钟)</label>
          <input 
            type="number" 
            v-model.number="settings.cacheDuration" 
            class="admin-form-input"
            min="0"
          >
          <div class="setting-desc">系统数据缓存时间，0表示不缓存</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">页面大小</label>
          <select v-model="settings.pageSize" class="admin-form-input">
            <option v-for="size in [10, 15, 20, 30, 50, 100]" :key="size" :value="size">
              {{ size }}
            </option>
          </select>
          <div class="setting-desc">列表页每页显示的条目数</div>
        </div>
        
        <div class="setting-item">
          <label class="setting-label">允许的文件类型</label>
          <div class="tag-input">
            <div class="tags">
              <span class="tag" v-for="(type, index) in settings.allowedFileTypes" :key="index">
                {{ type }}
                <span class="tag-remove" @click="removeFileType(index)">&times;</span>
              </span>
            </div>
            <div class="tag-add">
              <input 
                type="text" 
                v-model="newFileType" 
                class="admin-form-input"
                placeholder="添加文件类型，如jpg"
                @keyup.enter="addFileType"
              >
              <button class="admin-btn" @click="addFileType">添加</button>
            </div>
          </div>
          <div class="setting-desc">系统允许上传的文件类型</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminSettings',
  data() {
    return {
      loading: true,
      settings: {
        systemName: 'CarbonSpark环保平台',
        logoUrl: '',
        maintenanceMode: false,
        maintenanceMessage: '系统正在维护中，请稍后再试...',
        allowRegistration: true,
        requireApproval: false,
        defaultPoints: 10,
        challengeRequireReview: true,
        autoApproveSubmissions: false,
        maxTaskImages: 5,
        cacheDuration: 30,
        pageSize: 20,
        allowedFileTypes: ['jpg', 'jpeg', 'png', 'gif', 'pdf']
      },
      newFileType: ''
    }
  },
  created() {
    this.fetchSettings()
  },
  methods: {
    fetchSettings() {
      // 模拟API请求
      setTimeout(() => {
        // 实际应用中应该从API获取设置
        this.loading = false
      }, 1000)
    },
    saveSettings() {
      this.loading = true
      
      // 模拟API保存
      setTimeout(() => {
        alert('设置已保存')
        this.loading = false
      }, 1000)
    },
    addFileType() {
      if (this.newFileType && !this.settings.allowedFileTypes.includes(this.newFileType)) {
        this.settings.allowedFileTypes.push(this.newFileType)
        this.newFileType = ''
      }
    },
    removeFileType(index) {
      this.settings.allowedFileTypes.splice(index, 1)
    }
  }
}
</script>

<style scoped>
.admin-settings {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 0;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.settings-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.settings-section {
  padding: 24px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.section-title {
  margin-top: 0;
  margin-bottom: 24px;
  font-size: 18px;
  color: rgba(0, 0, 0, 0.85);
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.setting-item {
  margin-bottom: 24px;
}

.setting-item:last-child {
  margin-bottom: 0;
}

.setting-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.85);
}

.setting-desc {
  margin-top: 4px;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.logo-input {
  display: flex;
  gap: 8px;
}

.logo-preview {
  margin-top: 8px;
  max-width: 200px;
  padding: 8px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
}

.logo-preview img {
  max-width: 100%;
  height: auto;
}

/* 开关样式 */
.switch-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 30px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
}

input:checked + .slider {
  background-color: #1890ff;
}

input:focus + .slider {
  box-shadow: 0 0 1px #1890ff;
}

input:checked + .slider:before {
  transform: translateX(30px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

.switch-status {
  font-weight: 500;
}

/* 标签输入 */
.tag-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  font-size: 14px;
}

.tag-remove {
  margin-left: 6px;
  cursor: pointer;
  font-size: 16px;
}

.tag-add {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .logo-input {
    flex-direction: column;
  }
  
  .tag-add {
    flex-direction: column;
  }
}
</style> 